<template>
  <div>
    <el-card>
      <div slot="header">
        <my-breadcrumb>评论管理</my-breadcrumb>
      </div>

      <!-- 表格 -->
      <el-table
        :data="comments"
      >
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column width="100px" label="总评论数" prop="total_comment_count"></el-table-column>
        <el-table-column width="100px" label="粉丝评论数" prop="fans_comment_count"></el-table-column>

        <el-table-column width="100px" label="评论状态">
          <template slot-scope="scope">
            <!-- {{scope.row.comment_status}} -->
            <el-tag v-if="scope.row.comment_status">正常</el-tag>
            <el-tag v-else type="warning">关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="150px" label="操作">
          <template slot-scope="scope">
            <!-- 作用域插槽：scope.row保存着当前行的数据 -->
            <el-button size="mini" v-if="scope.row.comment_status" @click="hSwitch(scope.row)">关闭评论</el-button>
            <el-button size="mini" v-else type="danger" @click="hSwitch(scope.row)">打开评论</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
      background
      @current-change="hCurrentChange"
      layout="prev, pager, next"
      :total="total_count">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  name: 'comments',
  data () {
    return {
      total_count: 0, // 总条数
      comments: []
    }
  },
  created () {
    this.loadComments()
  },
  methods: {
    async doSwitch (row) {
      try {
        const res = await this.$axios({
          url: '/mp/v1_0/comments/status',
          method: 'PUT',
          params: {
            article_id: row.id.toString()
          },
          data: {
            // 在当前的状态上取反
            allow_comment: !row.comment_status
          }
        })
        console.log(res)
        // 更新页面
        // a. 再发一次请求
        // b. 直接在本地数据上更新
        row.comment_status = !row.comment_status

        this.$message.success('操作成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 用户点击了 打开|关闭 评论
    hSwitch (row) {
      console.log(row)
      // 当前是true，想要改成false
      if (row.comment_status) {
        this.$confirm('你确定要关闭吗？关了之后，是无法了评论了').then(() => {
          this.doSwitch(row)
        }).catch(() => {

        })
      } else {
        this.doSwitch(row)
      }
    },
    hCurrentChange (curPage) {
      // 1. 更新页码
      this.page = curPage
      // 2. 重发请求
      this.loadComments()
      // console.log(curPage)
    },
    buildParams () {
      const params = {
        page: this.page,
        response_type: 'comment'
      }
      return params
    },
    async loadComments () {
      try {
        // 获取条件
        const params = this.buildParams()
        // 发请求
        const res = await this.$axios({
          url: '/mp/v1_0/articles',
          method: 'GET',
          params
        })
        console.log(res)
        this.comments = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
